﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Library.Files
{
    public class FileStreamhelper
    {
        public static void Write(string path, string textContent)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write(textContent);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        public static void WriteLine(string path, string textContent)
        {
            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.WriteLine(textContent);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        /// <summary>
        /// 打开一个文件，向其中追加指定的字符串，然后关闭该文件。
        /// 如果文件不存在，此方法将创建一个文件，将指定的字符串写入文件，然后关闭该文件。
        /// </summary>
        /// <param name="path"></param>
        /// <param name="textContent"></param>
        public static void AppendAllText(string path, string textContent)
        {
            File.AppendAllText(path, textContent);
        }


        public static string ReadFile(string fileName)
        {
            string fileContext = File.ReadAllText(fileName);
            return fileContext;
        }
    }
}
